<?php

/**
 * * create url
 * @author DuongNV
 * @copyright Tan MA JSC
 * @category common function
 * @version 1.1
 */

/**
 *
 * @param unknown_type $tabeName
 * @param unknown_type $itemID
 * @param unknown_type $itemURL
 */
function updateDataType($tabeName, $itemID, $itemURL) {
	
	// update data
	$db = new DBAccess();
	
	$select = $db->selectOneByCause( $GLOBALS['dataTypeFullColArray'], DATATYPE, array(DATATYPE_TABLENAME => $tabeName, SQL_AND, DATATYPE_ITEMID => $itemID));
	
	if ($select != null && count( $select) > 0 && ! empty( $select[DATATYPE_ID])) {
		$select[DATATYPE_ITEMURL] = $itemURL;
		$select[DATATYPE_EDITBY] = $_SESSION[SESSION_USERNAME];
		
		if ($db->updateData( $select, DATATYPE)) {
			return true;
		}
	
	} else {
		$select = array();
		$select[DATATYPE_ID] = 0;
		$select[DATATYPE_TABLENAME] = $tabeName;
		$select[DATATYPE_ITEMID] = $itemID;
		$select[DATATYPE_ITEMURL] = $itemURL;
		if (isset( $_SESSION[SESSION_LANGUAGE])) {
			$select[DATATYPE_LANGUAGE] = $_SESSION[SESSION_LANGUAGE];
		} else {
			$select[DATATYPE_LANGUAGE] = DEFAULT_LANGAUGE;
		}
		$select[DATATYPE_EDITBY] = $_SESSION[SESSION_USERNAME];
		
		if ($db->insertData( $select, DATATYPE)) {
			return true;
		}
	}
	
	return false;
}

/**
 *
 * @param unknown_type $tabeName
 * @param unknown_type $itemID
 * @param unknown_type $itemURL
 */
function deleteDataType($tabeName, $itemID) {
	
	// update data
	$db = new DBAccess();
	
	$select = $db->selectOneByCause( $GLOBALS['dataTypeFullColArray'], DATATYPE, array(DATATYPE_TABLENAME => $tabeName, SQL_AND, DATATYPE_ITEMID => $itemID));
	
	$db->deleteData( DATATYPE, $select[DATATYPE_ID]);
	
	return true;
}

function identifyURL() {

	$db = new DBAccess();
	
	if (! isset( $_REQUEST[PARAM_URL])) { // trang chu
		
		return array(MODULE => HOMEPAGE, CATEGORY => null, TYPE => null);
		
	} else if ($_REQUEST[PARAM_URL] == "error") { // error page
		
		return array(MODULE => ERRORPAGE, CATEGORY => null, TYPE => null);
	
	} else if ($_REQUEST[PARAM_URL] == "san-pham") { // dich vu du lich
		
		return array(MODULE => PRODUCTS, CATEGORY => null, TYPE => CATEGORY_TYPE_PRODUCT);
	
	} else if ($_REQUEST[PARAM_URL] == "tai-ve") { // dich vu du lich
		
		return array(MODULE => FILES, CATEGORY => null, TYPE => CATEGORY_TYPE_COLLECTION);
	
	} else if ($_REQUEST[PARAM_URL] == "tim-kiem") { // dich vu du lich
		
		return array(MODULE => "SEARCH", CATEGORY => null, TYPE => null);
	
	} else {
		$dataType = $db->selectOneByCause( $GLOBALS['dataTypeFullColArray'], DATATYPE, array(DATATYPE_ITEMURL => $_REQUEST[PARAM_URL]));
		if ($dataType[DATATYPE_TABLENAME] == CATEGORIES) { // category
			
			$cate = $db->selectOneByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_URL => $_REQUEST[PARAM_URL]));
			
			if ($cate[CATEGORY_TYPE] == CATEGORY_TYPE_ACRTICLE) {
				
				return array(MODULE => ARTICLES, CATEGORY => $cate, TYPE => CATEGORY_TYPE_ACRTICLE);
			
			} else if ($cate[CATEGORY_TYPE] == CATEGORY_TYPE_PRODUCT) {
				
				return array(MODULE => PRODUCTS, CATEGORY => $cate, TYPE => CATEGORY_TYPE_PRODUCT);
			
			} else if ($cate[CATEGORY_TYPE] == CATEGORY_TYPE_COLLECTION) {
				
				return array(MODULE => FILES, CATEGORY => $cate, TYPE => CATEGORY_TYPE_COLLECTION);
			
			}
		
		} else { // ther
			return array(MODULE => $dataType[DATATYPE_TABLENAME], CATEGORY => null, TYPE => null);
		}
	}
}

/**
 * Change Frame URL Function
 * @param unknown_type $url
 */
function changeFrameURL($url) {

	header( "Location: $url");
}

/**
 * Change Document URL Function
 * @param unknown_type $url
 */
function changeDocumentURL($url) {

	echo "
    <script type=\"text/javascript\">
    <!--
    top.location = \"$url\"
    //-->
    </script>
    ";
}

/**
 *
 * @param unknown_type $lan
 * @param unknown_type $module
 * @param unknown_type $url
 * @return string
 */
function createURL($lan, $module, $url) {
	
	/* if ($module == URL_MODULE_CATEGORY) { $db = new DBAccess(); $cate = $db->selectOneByCause(
	 * array(CATEGORY_FID, CATEGORY_URL), CATEGORIES, array(CATEGORY_URL => $url)); if (empty(
	 * $cate[CATEGORY_FID])) { $url = $lan . "/" . $url . "/"; } else { } } else if ($module ==
	 * URL_MODULE_ARTICLE) { $db = new DBAccess(); $article = $db->selectOneByCause(
	 * array(ARTICLE_CATEGORYID), ARTICLES, array(ARTICLE_URL => $url)); $cate =
	 * $db->selectOneByCause( array(CATEGORY_URL, CATEGORY_FID), CATEGORIES, array(CATEGORY_ID =>
	 * $article[ARTICLE_CATEGORYID])); if (empty( $cate[CATEGORY_FID])) { $url = $lan . "/" .
	 * $cate[CATEGORY_URL] . "/" . $url . ".html"; } else { } } */
	
	return $lan . "/" . $url . ".html";
}

function createCateArray($cateID, $cateArray) {

	$db = new DBAccess();
	$selected = $db->selectOneByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_ID => $cateID));
	
	if ($selected != null && count( $selected) > 0 && ! empty( $selected[CATEGORY_ID])) {
		$cateArray[count( $cateArray)] = $selected;
		
		if (! empty( $selected[CATEGORY_FID])) {
			$cateArray = createCateArray( $selected[CATEGORY_FID], $cateArray);
		}
	}
	
	return $cateArray;
}

/**
 *
 * @param unknown_type $cateID
 * @return string
 */
function createPathWay($cateID, $lan = DEFAULT_LANGAUGE) {

	$pathway = "<ul class=\"path-way\"> <li><a href=\"\"><span class=\"home-icon\"></span></a></li>";
	
	$db = new DBAccess();
	$cateArray = array();
	$selected = $db->selectOneByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_ID => $cateID));
	
	if ($selected != null && count( $selected) > 0 && ! empty( $selected[CATEGORY_ID])) {
		$cateArray[0] = $selected;
		
		if (! empty( $selected[CATEGORY_FID])) {
			$cateArray = createCateArray( $selected[CATEGORY_FID], $cateArray);
		}
		
		$cateArray = array_reverse( $cateArray);
		
		foreach ( $cateArray as $cate ) {
			
			$cate[CATEGORY_URL] = createURL( $lan, CATEGORIES, $cate[CATEGORY_URL]);
			
			$pathway .= "<li><a href=\"" . $cate[CATEGORY_URL] . "\">" . $cate[CATEGORY_TITLE] . "</a></li>";
		}
	}
	
	$pathway .= "</ul>";
	
	return $pathway;
}

/**
 * decode param
 * @return multitype:
 */
function decodeParam() {

	$curUrl = curPageURL();
	$result = array();
	
	if (strpos( $curUrl, "?")) {
		$param = explode( "?", $curUrl);
		$param = $param[1];
		
		if (strpos( $param, "&")) {
			$paramArray = explode( "&", $param);
			
			foreach ( $paramArray as $value ) {
				$value = "1" . $value;
				if (strpos( $value, "color-")) {
					$color = explode( "color-", $value);
					if ($color[1] != "all") {
						$result[PARAM_COLOR] = array(KEY => LIKE, COLUMN => PRODUCT_COLORCODE, VALUE => $color[1]);
					}
				} else if (strpos( $value, "pricerg-")) {
					$pricerg = explode( "pricerg-", $value);
					if ($pricerg[1] != "all") {
						$result[PARAM_PRICERANGE] = $GLOBALS['priceRangeParram'][$pricerg[1]];
					}
				} else if (strpos( $value, "order-")) {
					$order = explode( "order-", $value);
					$result[PARAM_ORDER] = $GLOBALS['orderParram'][$order[1]];
				}
			}
		
		} else {
			$param = "1" . $param;
			if (strpos( $param, "color-")) {
				$color = explode( "color-", $param);
				if ($color[1] != "all") {
					$result[PARAM_COLOR] = array(KEY => LIKE, COLUMN => PRODUCT_COLORCODE, VALUE => $color[1]);
				}
			} else if (strpos( $param, "pricerg-")) {
				$pricerg = explode( "pricerg-", $param);
				if ($pricerg[1] != "all") {
					$result[PARAM_PRICERANGE] = $GLOBALS['priceRangeParram'][$pricerg[1]];
				}
			} else if (strpos( $param, "order-")) {
				$order = explode( "order-", $param);
				$result[PARAM_ORDER] = $GLOBALS['orderParram'][$order[1]];
			}
		}
		
		return $result;
	
	} else {
		return null;
	}
}

?>
